package com.caac.core.config;

import java.text.DecimalFormat;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright caac 2006</p>
 * <p>Company: caac</p>
 * @author <a href="mailto:project@caac.com.cn">Project Group</a>
 * @version 1.0
 */

public class Monitor {
    private static Monitor instance = new Monitor();
    public static Monitor getInstance() {
        return instance;
    }

    private static final int M = 1024 * 1024;
    private static final DecimalFormat FORMAT = new DecimalFormat("#0.00");
    private Runtime runtime;
    private String usedMemory;
    private String totalMemory;
    private String maxMemory;
    private String percentFree;
    private int activeThreadCount;
    private Monitor() {
        runtime = Runtime.getRuntime();
    }

    public void init() {
        double fm = (double) runtime.freeMemory() / M;
        double tm = (double) runtime.totalMemory() / M;
        double mm = (double) runtime.maxMemory() / M;
        double um = tm - fm;
        double pf = (fm / tm) * 100;
        usedMemory = FORMAT.format(um);
        totalMemory = FORMAT.format(tm);
        maxMemory = FORMAT.format(mm);
        percentFree = FORMAT.format(pf);
        activeThreadCount = Thread.activeCount();
    }

    public String getUsedMemory() {
        return usedMemory;
    }

    public String getTotalMemory() {
        return totalMemory;
    }

    public String getMaxMemory() {
        return maxMemory;
    }

    public String getPercentFree() {
        return percentFree;
    }

    public int getActiveThreadCount() {
        return activeThreadCount;
    }
}
